<template>
  <div class="real-time">
    <div class="header">今日实时收入</div>
    <div class="content">
      <div class="body">
        <div class="echart">
          <img src="./../../assets/icons/yinliang.svg" alt="" @click="incomeAudioPlay">
          <div class="circle">
            <div class="outer">
              <p class="money">{{incomeList.todayincome}}</p>
              <p class="text">今日总收入</p>
            </div>
          </div>
          <div class="echart-item">
            <div class="item cash">
              <span class="color color1"/>
              <span class="text">现金</span>
              <span class="money">{{incomeList.cashincome}}</span>
            </div>
            <div class="item pay">
              <span class="color color2"/>
              <span class="text">电子支付</span>
              <span class="money">{{incomeList.mobileincome}}</span>
            </div>
            <div class="item event">
              <span class="color color3"/>
              <span class="text">减免金额</span>
              <span class="money">{{incomeList.reductionincome}}</span>
            </div>
            <div class="item etc">
              <span class="color color4"/>
              <span class="text">ETC</span>
              <span class="money">{{incomeList.etcincome}}</span>
            </div>
          </div>
        </div>
        <div class="echart">
          <img src="./../../assets/icons/yinliang.svg" alt="" @click="serviceAreaAudioPlay">
          <div class="circle">
            <div class="outer">
              <p class="text">服务区概况</p>
            </div>
          </div>
          <div class="echart-item">
            <div class="item cash">
              <span class="color color1"/>
              <span class="text">总泊位数</span>
              <span class="money">{{serviceArea.parkingcount}}</span>
            </div>
            <div class="item pay">
              <span class="color color2"/>
              <span class="text">空泊位数</span>
              <span class="money">{{serviceArea.spackcount}}</span>
            </div>
            <div class="item event">
              <span class="color color3"/>
              <span class="text">利用率</span>
              <span class="money">{{serviceArea.utilization}}%</span>
            </div>
            <div class="item etc">
              <span class="color color4"/>
              <span class="text">占用率</span>
              <span class="money">{{serviceArea.free}}%</span>
            </div>
          </div>
        </div>
        <div class="echart">
          <img src="./../../assets/icons/yinliang.svg" alt="" @click="tollAudioPlay">
          <div class="circle">
            <div class="outer">
              <p class="money">{{tollIncome.sumincome}}</p>
              <p class="text">收费站收入</p>
            </div>
          </div>
          <div class="echart-item">
            <div class="item cash">
              <span class="color color1"/>
              <span class="text">现金</span>
              <span class="money">{{tollIncome.cashincome}}</span>
            </div>
            <div class="item pay">
              <span class="color color2"/>
              <span class="text">电子支付</span>
              <span class="money">{{tollIncome.mobileincome}}</span>
            </div>
            <div class="item event">
              <span class="color color3"/>
              <span class="text">减免金额</span>
              <span class="money">{{tollIncome.reductionincome}}</span>
            </div>
            <div class="item etc">
              <span class="color color4"/>
              <span class="text">ETC</span>
              <span class="money">{{tollIncome.etcincome}}</span>
            </div>
          </div>
        </div>
      </div>
    </div>

  </div>
</template>

<script>
import { getTotalIncome, getallserviceArea, getTollIncome } from './../../api/index'
import audioPlay from '../../utils/audioPlay'
export default {
  name: 'RealTime',
  data () {
    return {
      // 收入列表
      incomeList: {},
      // 所有服务区信息
      serviceArea: {},
      // 收费信息列表
      tollIncome: {},
      flag: true,
      flag1: true,
      flag2: true
    }
  },
  created () {
    // 总收入查询
    this.getTotalIncomeData()
    // 服务区信息查询
    this.getAllServiceAreaData()
    // 收费站收入函数调用
    this.getTollIncomeData()
    window.addEventListener('unload', this.incomeAudioPlay)
    // const utterThis = new window.SpeechSynthesisUtterance('你好，世界！')
    // window.speechSynthesis.speak(utterThis)
  },
  methods: {
    // 总收入查询
    async getTotalIncomeData () {
      const result = await getTotalIncome()
      // console.log(result)
      this.incomeList = result.data
    },
    // 获取服务区信息函数
    async getAllServiceAreaData () {
      const res = await getallserviceArea()
      // console.log(res)
      this.serviceArea = res.data
    },
    // 收费站收入概况
    async getTollIncomeData () {
      const result = await getTollIncome()
      // console.log(result)
      this.tollIncome = result.data
    },
    // 总收入语音播放功能
    incomeAudioPlay () {
      // 定义一个节流阀
      if (this.flag) {
        this.flag = false
        setTimeout(function () {
          this.flag = true
        }, 500)
        audioPlay(this.incomeList.broadcast)
      }
    },
    // 服务区语音播报
    serviceAreaAudioPlay () {
      if (this.flag1) {
        this.flag1 = false
        setTimeout(function () {
          this.flag1 = true
        }, 500)
        audioPlay(this.serviceArea.serivceInfoBroadcast)
      }
    },
    // 服务区语音播报
    tollAudioPlay () {
      if (this.flag2) {
        this.flag2 = false
        setTimeout(function () {
          this.flag2 = true
        }, 500)
        audioPlay(this.tollIncome.tollBroadcast)
      }
    }
  },
  destroyed () {
    // 页面切换时,销毁正在播放的语音
    // this.incomeAudioPlay()
    window.removeEventListener('unload', this.incomeAudioPlay)
  }
}
</script>

<style scoped lang="less">
  .real-time {
    letter-spacing: 1px;
    width: 100%;
    height: auto;
    padding-bottom: 44px;

    .header {
      position: relative;
      box-sizing: border-box;
      padding: 13px 0;
      text-align: center;
      font-size: 17.5px;
      color: #0a0a0a;
      font-weight: 600;
      background-color: #fff;
      border-bottom: 1px solid #e5e5e5;
    }

    .content {
      height: 100%;

      .title {
        position: relative;
        height: 37px;
        line-height: 37px;
        font-size: 14px;
        color: #979797;
        padding-left: 16px;
        background-color: #fff;

        img {
          position: absolute;
          top: 10px;
          left: 52px;
          content: '';
          display: block;
          width: 18px;
          height: 18px;
          transform: rotate(-90deg);
        }
      }

      .body {
        width: 100%;
        height: 100%;
        padding: 19px 15.5px 0;
        background-color: #f5f5f6;

        .echart {
          position: relative;
          width: 100%;
          height: 283px;
          background-color: #fff;
          overflow: hidden;
          margin-bottom: 15px;

          img {
            position: absolute;
            top: 0;
            right: 0;
            width: 24px;
            height: 24px;
          }

          &:nth-child(2) {
            .circle {
              .outer {
                display: flex;
                justify-content: center;
                align-items: center;
              }
            }
          }

          .circle {
            text-align: center;
            position: relative;
            width: 150px;
            height: 150px;
            margin: 33px auto 30px;
            background: linear-gradient(left, #a153fc, #6b31fa);
            background: -webkit-linear-gradient(left, #a153fc, #6b31fa);
            background: -ms-linear-gradient(left, #a153fc, #6b31fa);
            background: -webkit-linear-gradient(left, #a153fc, #6b31fa);
            border-radius: 50%;

            .outer {
              position: absolute;
              top: 50%;
              left: 50%;
              width: 100px;
              height: 100px;
              border-radius: 50%;
              transform: translate(-50%, -50%);
              background-color: #fff;

              .money {
                margin-top: 30px;
                margin-bottom: 10px;
                color: #323232;
                font-size: 20px;
                font-weight: 600;
              }

              .text {
                color: #000;
                font-size: 14px;
              }
            }
          }

          .echart-item {
            padding: 0 13px 0;

            .cash, .pay {
              margin-bottom: 15px;
            }

            .item {
              float: left;
              width: 50%;
              font-size: 14px;

              .color1 {
                background-color: #682dfa;
              }

              .color2 {
                background-color: #fc5333;
              }

              .color3 {
                background-color: #2eb4f6;
              }

              .color4 {
                background-color: #286fff;
              }

              .color {
                content: "";
                display: inline-block;
                width: 6px;
                height: 6px;
                margin-right: 8px;
                border-radius: 50%;
              }

              .text {
                margin-right: 10px;
                color: #c6c6c6;
              }

              .money {
                color: #4d4d4d;
              }
            }
          }
        }
      }
    }
  }
</style>
